/**
 * @file FlatShadedMode.cpp
 * @see FlatShadedMode
 * @see IRenderMode
 * @author M. Caputo
 * @date 2010-03-26
 */

#include <QtOpenGL>
#include "FlatShadedMode.h"

QString FlatShadedMode::name()
{
	return tr("Flat Shading Mode");
}

void FlatShadedMode::setSettings()
{
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);	
	glEnable(GL_LIGHT0);
}

void FlatShadedMode::unsetSettings()
{
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
}

void FlatShadedMode::draw(IScene *scene, const QColor & defaultColor)
{
	// The model color is always white, the color comes from the light source!
	glColor3f(1.0, 1.0, 1.0);
	
	const float lightPosition[4] = { 5.0, 5.0, 5.0, 1.0 };
	const float lightColor[4] = {
		defaultColor.redF(),
		defaultColor.greenF(),
		defaultColor.blueF(),
		1.0
	};
	
	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
	glEnable(GL_LIGHTING);
	
	int cp = scene->polygonsCount();
	for(int i=0; i<cp; i++) {
		CPolygon poly = scene->polygon(i);
		int cv = poly.verticesCount();
		glBegin( GL_POLYGON );
			for(int j=0; j<cv; j++) {
				CVertex vert = poly.vertex(j);
				const float *data = vert.vertex();
				glVertex3f(
					data[0],
					data[1],
					data[2]
				); 
			}
		glEnd();
	}
	
	glDisable(GL_LIGHTING);
}
